# -*- coding: utf-8 -*-
"""
@Time ： 2021/1/22 10:46
@Auth ： 张张呀
@File ：肯德基.py
@IDE ：PyCharm
@Motto：ABC(Always Be Coding)

"""
from tkinter import Tk, messagebox

import easygui
import requests

top = Tk()
top.withdraw()


class kendeji:
    def __init__(self):
        self.url = 'http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=keyword'
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36 Edg/87.0.664.75',
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
        }
        self.data = {
            'cname': '',
            'pid': '',
            'keyword': '',
            'pageIndex': 1,
            'pageSize': 10,
        }

    def write_txt(self, filename1, response):
        with open(filename1, 'w', encoding='utf-8') as fp:
            fp.write(str(response))
        messagebox.showinfo("提示", "抓取数据完成,详情请看txt文件")

    def run(self):
        area = easygui.multenterbox('肯德基餐厅查询', '一蓑烟雨任平生',
                                    fields=['请输入您要查询的餐厅关键字:'],
                                    values=[''])[0]

        self.data['keyword'] = area
        response = requests.post(self.url, self.data, self.headers).json()
        print(response)
        self.write_txt(area + '.txt', response)


if __name__ == '__main__':
    kendeji().run()
